package com.tmz.dao;

import java.util.Iterator;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.googlecode.s2hibernate.struts2.plugin.annotations.SessionTarget;
import com.googlecode.s2hibernate.struts2.plugin.annotations.TransactionTarget;
import com.tmz.domain.Apuesta;
import com.tmz.domain.Jornada;
import com.tmz.domain.Partido;
import com.tmz.domain.User;
import com.tmz.utils.QuinielaUtil;

public class ApuestaDAOImpl implements ApuestaDAO {
	@SessionTarget
	Session session;
	@TransactionTarget
	Transaction transaction;
	
	@SuppressWarnings("unchecked")
	public List<Apuesta> listApuestas() {
		List<Apuesta> courses = null;
		try {
			Query query = session.createQuery("from Apuesta");
			courses = query.list();
			System.out.println(courses.size());
		} catch (Exception e) {
			e.printStackTrace();
		} 
		return courses;
	}

	@Override
	public void saveApuesta(Apuesta a) {
		try {
			session.save(a);
		} catch (Exception e) {
			transaction.rollback();
			e.printStackTrace();
		} 
	}

	@SuppressWarnings("unchecked")
	public Apuesta getApuesta(Jornada j, User u) {
		List<Apuesta> courses = null;
		try {
			Query query = session.createQuery("from Apuesta where jornada_fk = ? and user_fk = ?");
			query.setLong(0, j.getId());
			query.setLong(1, u.getId());
			courses = query.list();			
		} catch (Exception e) {
			e.printStackTrace();
		} 
		return courses.get(0);
	}

}
